package com.example.demo.common;

/**
 * 静态代理
 *
 * @author liuhongfeng
 * @date 2019/2/10 11:32
 */
public class StaticProxyTest {

    interface Subject {
        void sayHi();
        void sayHello();
    }

    static class SubjectImpl implements Subject {

        @Override
        public void sayHi() {
            System.out.println("hi");
        }

        @Override
        public void sayHello() {
            System.out.println("hello");
        }
    }

    // 代理类 作用：用来包装SubjectImpl类的实例
    static class SubjectImplProxy implements Subject {
        private Subject target;

        // 有参构造
        public SubjectImplProxy(Subject target) {
            this.target = target;
        }

        @Override
        public void sayHi() {
            System.out.print("say:");
            target.sayHi();
        }

        @Override
        public void sayHello() {
            System.out.print("say:");
            target.sayHello();
        }
    }

    public static void main(String[] args) {
        Subject subject = new SubjectImpl();
        Subject subjectProxy = new SubjectImplProxy(subject);
        subjectProxy.sayHi();
        subjectProxy.sayHello();
    }

}
